package 简单.计算二叉树的直径;

public class Solution {
    int ans;
    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        maxdepth(root);
        return ans-1;

    }

    public int maxdepth(TreeNode root){
       if(root == null){
           return 0;
       }
       int l = maxdepth(root.left);
       int r = maxdepth(root.right);
       ans = Math.max(ans,l+r+1);
       return Math.max(l,r)+1;

    }

}
